package com.daybook.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.daybook.mapper.BooksMapper;
import com.daybook.pojo.Books;
import com.daybook.pojo.BooksAndCategory;
import com.daybook.pojo.Category;
import com.daybook.service.BooksService;
import com.daybook.util.PageHelp;
@Service
public class BooksServiceImpl implements BooksService {
	@Autowired
	private BooksMapper booksMapper;
	
	
	/****************用户**********************/
	//1.查询最新上架的书籍
	public List<Books> getBooksByNewTime( int count){
		return booksMapper.selectBooksByNewTime(count);
	}
	//2.销量排行榜
	public List<Books> getBooksByNewSaleNum( int count){
		return booksMapper.selectBooksByNewSaleNum(count);	}
	//3.主页类别查询
	public List<Books> getBooksByCname(String came){
		return booksMapper.selectBooksByCname(came);
	}
	//4.多条件查询书籍信息
	public PageHelp getBooksByCondition(Map<String, Object> map,Integer pageNow){
		 int totalCount=booksMapper.selectCountByCondition(map);//获取总的记录数
		    List<Books> booksList=new ArrayList<Books>();
		    PageHelp page=null;
		    if(pageNow!=null){
		        page=new PageHelp(totalCount, pageNow);
		        page.setPageSize(8);
		        map.put("startPos", page.getStartPos());
		        map.put("pageSize", page.getPageSize());
		        booksList=booksMapper.selectBooksByCondition(map);//从startPos开始，获取pageSize条数据
		    }else{
		        page=new PageHelp(totalCount, 1);//初始化pageNow为1
		        page.setPageSize(8);
		        map.put("startPos", page.getStartPos());
		        map.put("pageSize", page.getPageSize());
		        booksList=booksMapper.selectBooksByCondition(map);//从startPos开始，获取pageSize条数据
		    }
		    page.setList(booksList);
		    return page;
		
	}
	//5.根据ID查询单本书籍
		public BooksAndCategory getBookByBid(int id){
			return booksMapper.selectBookByBid(id);
		}
	/****************管理员**********************/	
	
	@Override
	public int addNewBook(Books books) {
		int result=-1;
		result=booksMapper.addNewBook(books);
		return result;
	}
	//分页查询书的信息
		public PageHelp getBooksPageList(Map<String, Object> map,Integer pageNow){
			 int totalCount=booksMapper.getBooksCount(map);//获取总的记录数
			    List<BooksAndCategory> booksList=new ArrayList<BooksAndCategory>();
			    PageHelp page=null;
			    if(pageNow!=null){
			        page=new PageHelp(totalCount, pageNow);
			        page.setPageSize(3);
			        map.put("startPos", page.getStartPos());
			        map.put("pageSize", page.getPageSize());
			        booksList=booksMapper.selectAllBookByPage(map);//从startPos开始，获取pageSize条数据
			    }else{
			        page=new PageHelp(totalCount, 1);//初始化pageNow为1
			        page.setPageSize(3);
			        map.put("startPos", page.getStartPos());
			        map.put("pageSize", page.getPageSize());
			        booksList=booksMapper.selectAllBookByPage(map);//从startPos开始，获取pageSize条数据
			    }
			    page.setList(booksList);
			    
			    return page;
		}
		
		
		 //修改书的状态
		public int changeBookState(int bid,int bstate){
			 return booksMapper.updateBookState(bid, bstate);
			 
		 }
		 //更新数据信息
		public int changeBookInfo(Books books){
			return booksMapper.updateBookInfo(books);
		 }
		 //编号验证
		public int getBnum(String bnum){
			 return booksMapper.getBnum(bnum);
		 }
		 //统计
		public List<Books> getTop9Books(){
			return booksMapper.selectTopBooks();
		}
}
